package com.alianzamedica.controllers.prescription;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.objectsearch.sqlsearch.ObjectSearch;
import org.objectsearch.web.tools.Converter;
import org.w3c.dom.Document;

import com.alianzamedica.businessobject.Patient;
import com.alianzamedica.controllers.constants.ActionConstants;
import com.alianzamedica.tools.Enviroment;

/**
 * @author carlos
 * 
 */
public class ShowPatientList extends Action {

	@SuppressWarnings("unchecked")
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Enviroment env = Enviroment.getInstance();
		Document doc = env.getDocument();
		Integer numPage = 1;
		try {
			numPage = Converter.string2Integer(request.getParameter("pageNum"));
			if (numPage == null) {
				numPage = 1;
			}
		} catch (Exception e) {
		}

		String patientName = request.getParameter("name");
		ObjectSearch search = new ObjectSearch(doc,
				"com.alianzamedica.connection.ConnectionImpl");
		search.setDefaultDialect(ObjectSearch.MYSQL_DIALECT);
		search.setLike(true);
		Patient p = new Patient();
		p.setFullName(patientName);
		Integer totalPages = search.getTotalPages(p);
		ArrayList list = search.getPagedQuery(p, null, numPage, ActionConstants.PAGE_SIZE);
		request.setAttribute("patients", list);
		request.setAttribute("totalPages", totalPages);

		return mapping.findForward("success");
	}

}
